//
//  MCPersonalTopView.swift
//  StoreManage
//
//  Created by MC on 2018/9/5.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

class MCPersonalTopView: UIView {

    public var dataDict = Dictionary<String, Any>() {
        didSet {
            let nickName = dataDict["nickName"] as? String ?? ""
            let picture = dataDict["picture"] as? String ?? ""
            let levelTemp = dataDict["level"] as? NSNumber ?? 0
            let levelTuples = MCLevelConversion(level: levelTemp.intValue)
            
            
            iconImageView.sd_setImage(with: URL.init(string: picture), placeholderImage: #imageLiteral(resourceName: "Reuse_placeholder_1_1"))
            nickNameLabel.text = nickName
            identityLabel.text = levelTuples.levelName
            
            
            if levelTuples.level == MCRole_manager {
                identityLabel.backgroundColor = UIColor.MCAssist_C
            } else if levelTuples.level == MCRole_assistant {
                identityLabel.backgroundColor = UIColor.MCAssist_B
            } else {
                identityLabel.backgroundColor = UIColor.MCAssist_F
            }
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.white
        self.frame = CGRect.init(x: 0, y: 0, width: 0, height: 160)
        
        self.addSubview(bgView)
        bgView.snp.remakeConstraints { (make) ->Void in
            make.left.right.top.equalTo(0)
            make.bottom.equalTo(-30)
        }
        
        bgView.addSubview(iconImageView)
        iconImageView.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(bgView)
            make.top.equalTo(bgView).offset(5)
            make.size.equalTo(CGSize.init(width: 60, height: 60))
        }
        
        bgView.addSubview(nickNameLabel)
        nickNameLabel.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(iconImageView)
            make.top.equalTo(iconImageView.snp.bottom).offset(15)
        }
        
        bgView.addSubview(identityLabel)
        identityLabel.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(bgView)
            make.centerY.equalTo(bgView.snp.bottom)
            make.size.equalTo(CGSize.init(width: 50, height: 16))
        }
        
        
        // 如果是分销员就隐藏编辑页面
        bgView.addSubview(editButton)
        editButton.snp.remakeConstraints { (make) ->Void in
            make.centerY.equalTo(nickNameLabel)
            make.left.equalTo(nickNameLabel.snp.right).offset(-5)
            make.size.equalTo(CGSize.init(width: 40, height: 40))
        }
        
        bgView.addSubview(editAreaButton)
        editAreaButton.snp.remakeConstraints { (make) ->Void in
            make.bottom.equalTo(nickNameLabel)
            make.left.equalTo(nickNameLabel).offset(-20)
            make.right.equalTo(nickNameLabel).offset(20)
            make.top.equalTo(iconImageView)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCMain
        return view
    }()
    
    public lazy var iconImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.layer.cornerRadius = 30
        imageView.layer.masksToBounds = true
        imageView.layer.borderColor = UIColor.MCGray_light.cgColor
        imageView.layer.borderWidth = 1
        imageView.image = #imageLiteral(resourceName: "Reuse_icon")
        
        return imageView
    }()
    
    public lazy var nickNameLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(18)
        label.textColor = UIColor.white
        return label
    }()
    
    public lazy var identityLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(10)
        label.backgroundColor = UIColor.orange
        label.layer.masksToBounds = true
        label.layer.cornerRadius = 8
        label.textColor = UIColor.white
        label.textAlignment = NSTextAlignment.center
        return label
    }()
    
    
    public lazy var editButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.setImage(#imageLiteral(resourceName: "Mine_edit"), for: .normal)
        return button
    }()
    
    // 对编辑按钮区域扩展
    public lazy var editAreaButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        return button
    }()
    
}
